/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.List;
import javax.ejb.Stateless;
import model.Recepit;
import model.User;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import model.GroupDefinition;
import model.Payment;

/**
 *
 * @author Alexandre
 */
@Stateless
public class UserActions implements UserActionsLocal {

    @PersistenceContext(unitName = "EnterpriseApplication1-ejbPU")
    EntityManager em;

    public boolean addNewUser(String username, String email, String password) {
        try {
            User u = new User(username, email, password);
            if (u != null) {
                em.persist(u);
                return true;
            }
        } catch (Exception ex) {
            int a = 0;
        }
        return false;
    }

    public User loginUser(String username, String password) {
        User u = em.find(User.class, username);
        if (u != null) {
            if (u.getPassword() != null && u.getPassword().equals(password)) {
                return u;
            }
        }
        return null;
    }

    public boolean addReceipt(User user, String category, String concept, float quantity, int groupId) {
        //TODO ESE 1 no mola nada y el -1!
        Recepit receipt = new Recepit(1, concept, 1, quantity, new Date(), -1);        
        receipt.setGroupId(em.find(GroupDefinition.class, groupId));
        if (user != null) {
            user.getRecepitCollection().add(receipt);
            em.persist(user);
            return true;
        }
        return false;
    }

    @Override
    public GroupDefinition joinGroup(User user, int groupId) {
        GroupDefinition group = em.find(GroupDefinition.class, groupId);
        if(group != null){
            group.getUserCollection().add(user);
            em.persist(group);
            return group;
        }
        return null;
    }

    @Override
    public GroupDefinition getGroupPermission(User user, int groupId) {
        if(user.isAllowed(groupId)){
            return user.getGroup(groupId);
        }
        return null;
    }

    @Override
    public boolean createNewGroup(User user, String namegroup) {
        try{

        GroupDefinition group = new GroupDefinition();
        group.setCreator(user);
        group.setName(namegroup);
        group.setPassword("a");
        group.setDescription("b");
        Collection<User> g1 = group.getUserCollection();
        g1.add(user);
        group.setUserCollection(g1);
        Collection<GroupDefinition> groupList = user.getGroupDefinitionCollection();
        groupList.add(group);
        
        User u2 = em.find(User.class, user.getUsername());
        u2.setGroupDefinitionCollection(groupList);        
        user.setGroupDefinitionCollection(groupList);
        //em.refresh(user);
        em.refresh(u2);
        em.persist(group);

        return true;
        }catch(ConstraintViolationException ex){
            Set<ConstraintViolation<?>> list = ex.getConstraintViolations();
            int a = 0;
        }catch(Exception ex){
            int a = 0;
        }
        return false;
    }

    @Override
    public boolean addPayment(User user, String target, float quantity, int groupId) {
        Payment pay = new Payment(1, new Date(), String.valueOf(quantity), -1);        
        pay.setGroupId(em.find(GroupDefinition.class, groupId));
        if (user != null) {
            user.getPaymentCollection().add(pay);
            em.persist(user);
            return true;
        }
        return false;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
